package com.feng.rabbitmq.controller;

import com.feng.rabbitmq.config.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * confrim
 * 生产者发送消息
 */
@Slf4j
@RestController
@RequestMapping("confirm")
public class ProducerController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    //发消息
    /** 交换机错误：
     * 2021-12-13 22:14:02.257  INFO 9288 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
     * 2021-12-13 22:14:02.289  INFO 9288 --- [nio-8080-exec-1] c.f.r.controller.ProducerController      : 1111-发送消息为：大家伙
     * 2021-12-13 22:14:02.295  INFO 9288 --- [nectionFactory1] com.feng.rabbitmq.config.MyCallBack      : 交换机已经收到id为1的消息
     * 2021-12-13 22:14:02.295  INFO 9288 --- [nio-8080-exec-1] c.f.r.controller.ProducerController      : 2222-发送消息为：大家伙
     * 2021-12-13 22:14:02.300 ERROR 9288 --- [168.88.128:5672] o.s.a.r.c.CachingConnectionFactory       : Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'confirm_exchange2222' in vhost '/', class-id=60, method-id=40)
     * 2021-12-13 22:14:02.301  INFO 9288 --- [ntContainer#0-1] c.f.rabbitmq.consumer.ConfrimConsumer    : 接收的消息是：大家伙1111
     * 2021-12-13 22:14:02.301  INFO 9288 --- [nectionFactory2] com.feng.rabbitmq.config.MyCallBack      : 交换机还没有收到id为2的消息，是因为：channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'confirm_exchange2222' in vhost '/', class-id=60, method-id=40)
     * @param msg
     *
     * 路由key错误：交换机可以收到消息，配置了回退，就可以回退
     * 2021-12-13 22:20:36.482  INFO 6196 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
     * 2021-12-13 22:20:36.520  INFO 6196 --- [nio-8080-exec-1] c.f.r.controller.ProducerController      : 1111-发送消息为：大家伙1111
     * 2021-12-13 22:20:36.525  INFO 6196 --- [nectionFactory1] com.feng.rabbitmq.config.MyCallBack      : 交换机已经收到id为1的消息
     * 2021-12-13 22:20:36.525  INFO 6196 --- [nio-8080-exec-1] c.f.r.controller.ProducerController      : 2222-发送消息为：大家伙2222
     * 2021-12-13 22:20:36.525  INFO 6196 --- [nio-8080-exec-1] c.f.r.controller.ProducerController      : 3333-发送消息为：大家伙3333
     * 2021-12-13 22:20:36.529  INFO 6196 --- [nectionFactory1] com.feng.rabbitmq.config.MyCallBack      : 消息(Body:'大家伙3333' MessageProperties [headers={spring_returned_message_correlation=3}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, deliveryTag=0])，被交换机confirm_exchange退回，退回原因是：NO_ROUTE，路由Key: confirm_routingkey3333
     * 2021-12-13 22:20:36.529  INFO 6196 --- [nectionFactory2] com.feng.rabbitmq.config.MyCallBack      : 交换机已经收到id为3的消息
     * 2021-12-13 22:20:36.530  INFO 6196 --- [ntContainer#0-1] c.f.rabbitmq.consumer.ConfrimConsumer    : 接收的消息是：大家伙1111
     * 2021-12-13 22:20:36.532 ERROR 6196 --- [168.88.128:5672] o.s.a.r.c.CachingConnectionFactory       : Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'confirm_exchange2222' in vhost '/', class-id=60, method-id=40)
     * 2021-12-13 22:20:36.533  INFO 6196 --- [nectionFactory1] com.feng.rabbitmq.config.MyCallBack      : 交换机还没有收到id为2的消息，是因为：channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'confirm_exchange2222' in vhost '/', class-id=60, method-id=40)
     */
    @GetMapping("/sendMessage/{message}")
    public void sendMessage(@PathVariable("message")String msg){
//        交换机已经收到id为1的消息
        CorrelationData correlationData = new CorrelationData("1");
        CorrelationData correlationData2 = new CorrelationData("2");
        CorrelationData correlationData3 = new CorrelationData("3");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME,ConfirmConfig.CONFIRM_ROUTING_KEY,msg+"1111",correlationData);
        log.info("1111-发送消息为：{}",msg+"1111");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME+"2222",ConfirmConfig.CONFIRM_ROUTING_KEY,msg+"2222",correlationData2);
        log.info("2222-发送消息为：{}",msg+"2222");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME,ConfirmConfig.CONFIRM_ROUTING_KEY+"3333",msg+"3333",correlationData3);
        log.info("3333-发送消息为：{}",msg+"3333");
    }
}
